// 源码网址：https://raw.gitcode.com/Xuan_er123/proxy.pac/raw/main/proxy.pac

function FindProxyForURL(url, host) {
    // 强制代理域名列表
    var forcedProxyDomains = ["google.com", "youtube.com"];
    
    // 直连域名列表
    var domesticDomains = [
        "localhost",
        ".cn",
        // 苹果组
        "apple.com",
        "icloud.com",
        // 腾讯组
        "qq.com",
        "tencent.com",
        // 阿里巴巴组
        "tmall.com",
        "taobao.com",
        "ele.me", // 饿了么
        "dingtalk.com",
        "alipay.com",
        "alibaba.com",
        // 网易组
        "163.com",
        "netease.com",
        "youdao.com",
        "163yun.com",
        // 字节跳动组
        "douyin.com",
        "toutiao.com",
        // 京东组
        "jd.com",
        // 美团组
        "meituan.com",
        "maoyan.com", // 猫眼电影
        "dianping.com", // 大众点评
        // 爱奇艺组
        "iqiyi.com",
        "iqiyi.net",
        // 知乎组
        "zhihu.com",
        // 华为组
        "vmall.com",
        "gitcode.com",
        "huawei-3com.com",
        "huawei.com",
        "huaweiacad.com",
        "huaweiapaas.com",
        "huaweicloud",
        "huaweicloudapis.com",
        "huaweicloudlive.com",
        "huaweicloudsite.com",
        "huaweicloudwaf.com",
        "huaweidevice.com",
        "huaweidun.com",
        "huaweifile.com",
        "huaweiief.com",
        "huaweiita.com",
        "huaweils.com",
        "huaweimall.com",
        "huaweimarine.com",
        "huaweimarketplace.com",
        "huaweimossel.com",
        "huaweioneaccess.com",
        "huaweirtc.com",
        "huaweisafedns.com",
        "huaweishop.net",
        "huaweisoft.com",
        "huaweisre.com",
        "huaweistatic.com",
        "huaweiuniversity.com",
        "huaweiyun.com",
        "huaweizdl.com",
        //bilibili组
        "biliapi.com",
        "biliapi.net",
        "biliapi.xyz",
        "bilibil.com",
        "bilibili.cc",
        "bilibili.co",
        "bilibili.com",
        "bilibili.li",
        "bilibili.net",
        "bilibili996.com",
        "bilibiligame.co",
        "bilibiligame.net",
        "bilibilihelper.com",
        "bilibilipay.com",
        "bilicdn1.com",
        "bilicdn2.com",
        "bilicdn3.com",
        "bilicdn4.com",
        "bilicdn5.com",
        "bilicomic.com",
        "bilicomics.com",
        "biligame.co",
        "biligame.com",
        "biligame.net",
        "biligo.com",
        "biligx.com",
        "bilihot.com",
        "biliimg.com",
        "biliintl.co",
        "biliintl.com",
        "bilimanga.net",
        "bilinovel.com",
        "biliui.com",
        "bilive.com",
        "bilivideo.com",
        "bilivideo.net",
        "biliyu.com",
        // 微博组
        "weibo.co",
        "weibo.com",
        "weibo.tv",
        "weibocdn.com",
        "weibochem.com",
        "weibohelper.com",
        "weiboi.com",
        "weibolj.com",
        "weibomingzi.com",
        "weibopay.com",
        "weiboreach.com",
        "weibosci.com",
        "weiboums.com",
        "weibowang.net",
        "weiboyi.com",
        "weibozn.com",
        // 新浪组
        "sina-cf.com",
        "sina.com",
        "sina.lt",
        "sina.net",
        "sinaapp.com",
        "sinacdn.com",
        "sinacloud.com",
        "sinacloud.net",
        "sinadaxue.com",
        "sinaedge.com",
        "sinaemc.com",
        "sinaft.com",
        "sinaif.com",
        "sinaimg.com",
        "sinajs.com",
        "sinalog.com",
        "sinaluming.com",
        "sinan.fun",
        "sinanet.com",
        "sinanya.com",
        "sinaquyong.com",
        "sinas3.com",
        "sinas3.net",
        "sinashow.com",
        "sinastorage.com",
        "sinasws.com",
        "sinauda.com",
        "sinawap.com",
        "sinawf.com",
        // 百度组
        "baidu",
        "baidu-bank.com",
        "baidu-cdn.com",
        "baidu-cdn.net",
        "baidu-int.com",
        "baidu-itm.com",
        "baidu-mgame.com",
        "baidu-nj.com",
        "baidu-tech.com",
        "baidu-wenxue.com",
        "baidu.cc",
        "baidu.cm",
        "baidu.com",
        "baidu.mobi",
        "baidu.to",
        "baidu120.cc",
        "baidu123.com",
        "baidu521.com",
        "baiduads.com",
        "baiduapp.com",
        "baidubaidubaidu.net",
        "baidubce.com",
        "baidubcr.com",
        "baidubos.com",
        "baiducontent.com",
        "baidudaquan.com",
        "baidudw.com",
        "baidufcjx.com",
        "baidufe.com",
        "baidufree.com",
        "baiduhtml5.com",
        "baiduhui.com",
        "baidulook.com",
        "baiduor.com",
        "baidupan.com",
        "baidupcs.com",
        "baidupcs.net",
        "baidupeixun.com",
        "baidusmartapps.com",
        "baidusobing.com",
        "baidustat.com",
        "baidustatic.com",
        "baidusx.cc",
        "baidusx.com",
        "baidutab.com",
        "baidutieba.com",
        "baidutt.com",
        "baiduux.com",
        "baiduwebgame.com",
        "baiduwpan.com",
        "baiduxiaodai.com",
        "baiduxiaoshuo.com",
        "baiduyangguang.org",
        "baiduyun.com",
        "baiduyun.wiki",
        "baiduyuncdn.com",
        "baiduyuncdn.net",
        "baiduyundns.com",
        "baiduyundns.net",
        "baiduyunsousou.com",
        "baiduzjn.com",
        //其它
        "deepseek.com",
        "xuaner.icu",
        "dynv6.net",
        "cn.bing.com",
        "microsoft.com",
        "windows.com",
        "sharepoint.com",
        "adobe.com",
        "inftab.com",
        "live.com",
        "skype.com",
        "msftconnecttest.com",
        "office.com",
        "msn.com",
        "bing.net",
        "azureedge.net",
        "azure.com",
        "visualstudio.com",
        "vscode-cdn.net",
        "exp-tas.com",
        "microsoftonline.com",
        "windows.net",
        "docker.sunzishaokao.com",
        "docker.xuanyuan.me",
        "docker.1ms.run",
        "docker.1panel.live",
        "hub.rat.dev",
        "docker.wanpeng.top",
        "doublezonline.cloud",
        "docker.mrxn.net",
        "docker.anyhub.us.kg",
        "dislabaiot.xyz",
        "docker.fxxk.dedyn.io",
        "docker-mirror.aigc2d.com",
        "raindrop.io",
        "bing.com",
        "miaolianyun.my",
        "sharepoint.com",
        "wallspic.com",
        "xn--ngstr-lra8j.com",
    ];
    
    // 直连的IPv4地址段（内网+国内公网IP段）
    var ipv4Domestic = [
        "223.5.5.5/32",
        "223.6.6.6/32",
        "119.29.29.29/32",
        "1.12.12.12/32",
        "120.53.53.53/32",
        "180.76.76.76/32",
        "114.114.114.114/32",
        "114.114.115.115/32",
        "180.184.1.1/32",
        "180.184.2.2/32",
        "101.226.4.6/32",
        "218.30.118.6/32",
        "123.125.81.6/32",
        "140.207.198.6/32",
        "10.0.0.0/8",
        "172.16.0.0/12",
        "192.168.0.0/16",
        "127.0.0.0/8",
    ];
    
    // 直连的IPv6地址段（内网+国内公网IP段）
    var ipv6Domestic = [
        "2402:4e00::/128",
        "2402:4e00:1::/128",
        "2400:3200::1/128",
        "2400:3200:baba::1/128",
        "2400:da00::6666/128",
        "fc00::/7",  // 唯一本地地址
        "fe80::/10", // 链路本地地址
        "::1/128", //内网回环
        "240e::/12", //中国电信地址段
        "2408::/12", //中国联通地址段
        "2409::/12", //中国移动地址段
        "240a:4000::/21", //中国广电地址段
        "2001:da8::/32", // 中国教育网地址段
        "2402:4e00::/24", //腾讯地址段
        "2400:3200::/24", //阿里云地址段
        "240c::/32" //下一代互联网中心地址段
    ];
    
    // 1. 强制代理域名匹配（优先级最高）
    for (var i = 0; i < forcedProxyDomains.length; i++) {
        if (dnsDomainIs(host, forcedProxyDomains[i]) || host.endsWith("." + forcedProxyDomains[i])) {
            return "SOCKS 192.168.3.15:10080; PROXY 192.168.3.15:10081"; // 走代理
        }
    }

    // 2. 直连域名匹配
    for (var j = 0; j < domesticDomains.length; j++) {
        if (dnsDomainIs(host, domesticDomains[j])) {
            return "DIRECT"; // 直连
        }
    }
    
    // 3. IP地址规则匹配
    var ip = dnsResolve(host);
    if (ip) {
        // IPv4匹配
        if (ip.indexOf('.') !== -1) {
            for (var i = 0; i < ipv4Domestic.length; i++) {
                var net = ipv4Domestic[i].split('/');
                if (isInNet(ip, net[0], genMask(net[1]))) {
                    return "DIRECT";
                }
            }
        }
        // IPv6匹配
        else if (ip.indexOf(':') !== -1) {
            for (var j = 0; j < ipv6Domestic.length; j++) {
                var cidr = ipv6Domestic[j];
                if (isInNet6(ip, cidr)) {
                    return "DIRECT";
                }
            }
        }
    }
    
    // 4. 默认规则
    return "SOCKS 192.168.3.15:10080; PROXY 192.168.3.15:10081";
    
    // === 工具函数 ===
    // 生成IPv4掩码
    function genMask(prefix) {
        prefix = parseInt(prefix, 10);
        var mask = 0xFFFFFFFF << (32 - prefix);
        return [(mask >>> 24) & 0xFF, (mask >>> 16) & 0xFF, (mask >>> 8) & 0xFF, mask & 0xFF].join('.');
    }
    
    // IPv6 CIDR匹配
    function isInNet6(ip, cidr) {
        try {
            var [net, prefix] = cidr.split('/');
            prefix = parseInt(prefix || "128", 10);
            
            // 标准化IPv6地址
            ip = expandIPv6(ip);
            net = expandIPv6(net);
            
            // 比较前prefix位
            var ipParts = ip.split(':').flatMap(part => 
                [parseInt(part.substring(0, 2), 16), parseInt(part.substring(2), 16)]
            );
            
            var netParts = net.split(':').flatMap(part => 
                [parseInt(part.substring(0, 2), 16), parseInt(part.substring(2), 16)]
            );
            
            for (var i = 0; i < Math.floor(prefix/8); i++) {
                if (ipParts[i] !== netParts[i]) return false;
            }
            
            if (prefix % 8 !== 0) {
                var mask = 0xFF << (8 - (prefix % 8));
                var idx = Math.floor(prefix/8);
                return (ipParts[idx] & mask) === (netParts[idx] & mask);
            }
            
            return true;
        } catch(e) {
            return false;
        }
    }
    
    // 扩展缩写IPv6地址
    function expandIPv6(ip) {
        if (ip.indexOf('::') !== -1) {
            var parts = ip.split('::');
            var left = parts[0] ? parts[0].split(':') : [];
            var right = parts[1] ? parts[1].split(':') : [];
            var zeros = 8 - (left.length + right.length);
            var expanded = left.concat(Array(zeros).fill('0000')).concat(right);
            return expanded.map(part => part.padStart(4, '0')).join(':');
        }
        return ip.split(':').map(part => part.padStart(4, '0')).join(':');
    }
}